/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package src;

import java.awt.Color;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * Ελέγχει ένα JTextField αν περιέχει έναν θετικό ακέραιο
 * 
 */
public class PositiveIntInputVerifier extends InputVerifier {
     private final JLabel validationLabel;

    public PositiveIntInputVerifier(JLabel validationLabel) {
        super();
        this.validationLabel = validationLabel;
    }
     
    @Override
    public boolean verify(JComponent input) {
        if (((JTextField)input).getText().trim().equals(General.Empty))
        {
            ((JTextField)input).setText(General.Empty);
            this.validationLabel.setForeground(Color.black);
            return true;
        }
        int x =0;
        try {
             x= Integer.parseInt(((JTextField)input).getText());
        } catch (Exception e) {
            this.validationLabel.setForeground(Color.red);
            return false;
        }
        boolean result = (x> 0);
        if (!result)
            this.validationLabel.setForeground(Color.red);
        else
            this.validationLabel.setForeground(Color.black);
        return result;
    }
    
}
